package com.hzcx.app.simplechat.ui.login;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gavin.com.library.PowerfulStickyDecoration;
import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;
import com.gavin.com.library.listener.PowerGroupListener;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.login.adapter.PhoneCityAdapter;
import com.hzcx.app.simplechat.ui.login.bean.PhoneCityBean;
import com.hzcx.app.simplechat.ui.login.contract.PhoneCityContract;
import com.hzcx.app.simplechat.ui.login.event.PhoneCityEvent;
import com.hzcx.app.simplechat.ui.login.presenter.PhoneCityPresenter;
import com.hzcx.app.simplechat.util.dp.DpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 手机号地区选择
 */
public class PhoneCityActivity extends BaseActivity<PhoneCityPresenter> implements PhoneCityContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_phone)
    RecyclerView rvPhone;

    private List<PhoneCityBean.CityBean> listData;
    private PhoneCityAdapter phoneCityAdapter;
    private StickyDecoration decoration;

    @Override
    public int getLayout() {
        return R.layout.activity_phone_city;
    }

    @Override
    public void initPresenter() {
        mPresenter = new PhoneCityPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("选择地区");

        listData = new ArrayList<>();
        phoneCityAdapter = new PhoneCityAdapter(listData);

        GroupListener groupListener = position -> listData.get(position).getInitials();

        decoration = StickyDecoration.Builder
                .init(groupListener)
                .setGroupHeight(DpUtils.dp2px(this, 25))
                .setGroupBackground(getColor(R.color.color_F2F3F4))
                .setGroupTextColor(getColor(R.color.color_282F3E))
                .setGroupTextSize(DpUtils.dp2px(this, 16))
                .setTextSideMargin(DpUtils.dp2px(this, 25))
                .build();
        rvPhone.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvPhone.addItemDecoration(decoration);
        rvPhone.setAdapter(phoneCityAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getPhoneCity(this);

        phoneCityAdapter.setOnItemClickListener((adapter, view, position) -> {
            EventBus.getDefault().post(new PhoneCityEvent(listData.get(position)));
            finish();
        });
    }

    /**
     *  数据获取成功
     * @param data
     */
    @Override
    public void phoneCityResult(List<PhoneCityBean> data) {
        for (PhoneCityBean bean : data) {
            if (bean!=null&&bean.getCity() != null) {
                listData.addAll(bean.getCity());
            }
        }
        phoneCityAdapter.notifyDataSetChanged();
    }
}
